<template>
  <div class="app-container">
    <el-table
      v-loading="loading"
      :data="transList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="订单ID" align="center" prop="orderId" />
      <el-table-column label="交易号" align="center" prop="tradeNo" />
      <el-table-column label="交易类型" align="center" prop="tranType">
        <template #default="scope">
          <span v-if="scope.row.tranType === -1">微信支付</span>
          <span v-if="scope.row.tranType === 1">微信退款</span>
        </template>
      </el-table-column>
      <el-table-column label="交易额" align="center">
        <template #default="scope">
          {{ scope.row.tranAmount / 100 }}
        </template>
      </el-table-column>
      <el-table-column label="交易状态" align="center" prop="tranStatus">
        <template #default="scope">
          <span v-if="scope.row.tranStatus === 0">未交易</span>
          <span v-if="scope.row.tranStatus === 1">交易成功</span>
          <span v-if="scope.row.tranStatus === 2">交易失败</span>
        </template>
      </el-table-column>
      <el-table-column
        label="交易失败原因"
        align="center"
        prop="failedReason"
      />
      <el-table-column
        label="交易时间"
        align="center"
        width="160"
        prop="createTime"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:orderTrans:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:orderTrans:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page.sync="queryParams.pageNum"
      v-model:limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商城订单微信交易对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="交易号：微信平台产生" prop="tradeNo">
          <el-input
            v-model="form.tradeNo"
            placeholder="请输入交易号：微信平台产生"
          />
        </el-form-item>
        <el-form-item label="交易额" prop="tranAmount">
          <el-input v-model="form.tranAmount" placeholder="请输入交易额" />
        </el-form-item>
        <el-form-item label="交易失败原因" prop="failedReason">
          <el-input
            v-model="form.failedReason"
            placeholder="请输入交易失败原因"
          />
        </el-form-item>
        <el-form-item label="创建人ID" prop="createId">
          <el-input v-model="form.createId" placeholder="请输入创建人ID" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="OrderTrans">
import {
  listOrderTrans,
  getOrderTrans,
  delOrderTrans,
  addOrderTrans,
  updateOrderTrans,
} from "@/api/order/orderTrans";

const { proxy } = getCurrentInstance();

// 遮罩层
const loading = ref(true);
// 选中数组
const ids = ref([]);
// 非单个禁用
const single = ref(true);
// 非多个禁用
const multiple = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 商品分类表格数据
const transList = ref([]);
// 弹出层标题
const title = ref("");
// 是否显示弹出层
const open = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商城订单微信交易列表 */
const getList = () => {
  loading.value = true;
  listOrderTrans(queryParams.value).then((response) => {
    transList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
};
// 取消按钮
const cancel = () => {
  open.value = false;
  reset();
};
// 表单重置
const reset = () => {
  form.value = {
    id: null,
    orderId: null,
    tradeNo: null,
    tranType: null,
    tranAmount: null,
    tranStatus: null,
    failedReason: null,
    createBy: null,
    createTime: null,
    createId: null,
  };
  proxy.resetForm("form");
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryForm");
  handleQuery();
};
// 多选框选中数据
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
};
/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  open.value = true;
  title.value = "添加商城订单微信交易";
};
/** 修改按钮操作 */
const handleUpdate = (row) => {
  this.reset();
  const id = row.id || ids.value;
  getOrderTrans(id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改商城订单微信交易";
  });
};
/** 提交按钮 */
const submitForm = () => {
  proxy.$refs["form"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateOrderTrans(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          proxy.open = false;
          proxy.getList();
        });
      } else {
        addOrderTrans(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
};
/** 删除按钮操作 */
const handleDelete = (row) => {
  const ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除商城订单微信交易编号为"' + ids + '"的数据项？')
    .then(function () {
      return delOrderTrans(ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
};
/** 导出按钮操作 */
const handleExport = () => {
  this.download(
    "order/trans/export",
    {
      ...queryParams.value,
    },
    `trans_${new Date().getTime()}.xlsx`
  );
};

getList();
</script>
